#include "comm.hpp"

using namespace std;

bool MakeFifo()
{
    int n = mkfifo(FILENAME, 0666);
    if(n < 0)
    {
        cerr << "errno: " << errno << ", strerrno: " << strerror(errno) << endl;
        return false;
    }

    cout << "mkfifo success ... ... read" << endl;
    return true;
}

int main()
{
Start:
    int rfd = open(FILENAME, O_RDONLY);
    if(rfd < 0)
    {
        cerr << "errno: " << errno << ", strerrno: " << strerror(errno) << endl;
        if(MakeFifo()) goto Start;
        else
        {
            return 1;
        }

    }

    cout << "open fifo success" << endl;
    

    char buffer[1024];
    while(true)
    {
        ssize_t s = read(rfd, buffer, sizeof(buffer)-1);
        if(s > 0)
        {
            buffer[s] = 0;
            cout << "Client say# " << buffer << endl;
        }
    }

    close(rfd);

    return 0;
}